//
//  HXRevertQuestionListViewController.m
//  KD
//
//  Created by ifly on 16/3/16.
//  Copyright © 2016年 Whx. All rights reserved.
//

#import "HXRevertQuestionListViewController.h"

@interface HXRevertQuestionListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong) NSMutableArray *dataMArray;
/**消息通知页数*/
@property(nonatomic,assign) NSInteger index;
@property(nonatomic,strong) UITableView *tableView;
@end

@implementation HXRevertQuestionListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"revertCell"];
    self.tableView.mj_header=[AWRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshData)];
    self.tableView.mj_footer=[AWRefreshFooder footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    self.tableView.mj_footer.automaticallyChangeAlpha = YES;
    self.tableView.mj_footer.automaticallyHidden = NO;
    [self.tableView.mj_header beginRefreshing];
    self.tableView.tableFooterView = [[UIView alloc]init];
}

-(void)refreshData{
    if ([GlobalUtil isNilValue:[UserDefaultHandler getUserDetault:kID]]) {
        [SVProgressHUD showErrorWithStatus:@"请登录"];
        return;
    }
    [NetworkAPI revertListWithQuestID:self.quesdata.ID
                               UserId:[UserDefaultHandler getUserDetault:kID]
                             UserKind:[NSString stringWithFormat:@"%d",UserTypeManage]
                              PageNum:[NSString stringWithFormat:@"%ld",(long)self.index]
                                PageSize:@"10"
                              success:^(HXRevertQuestionListModel *result) {
                                  [self.tableView.mj_header endRefreshing];
                                  if ([result.code isEqualToString:@"200"]) {
                                      self.index++;
                                      [SVProgressHUD showSuccessWithStatus:@"获取成功"];
                                      [self.dataMArray removeAllObjects];
                                      [self.dataMArray addObjectsFromArray:result.anslist];
                                      [self.tableView reloadData];
                                      if ([result.anslist count] < 10) {
                                          [self.tableView.mj_header endRefreshing];
                                          [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                          self.tableView.mj_footer.alpha = 1.0f;
                                          return;
                                      }
                                  }else if([result.code isEqualToString:@"400"]){//暂无数据
                                      self.tableView.mj_footer.alpha = 1.0f;
                                      [self.tableView.mj_header endRefreshing];
                                      [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                  }else{//查询失败
                                      self.tableView.mj_footer.alpha = 0.0f;
                                      [self.tableView.mj_header endRefreshing];
                                      [self.tableView.mj_footer endRefreshing];
                                  }
                              } failure:^(NSError *err) {
                                  [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                  self.tableView.mj_footer.alpha = 0.0f;
                                  [self.tableView.mj_header endRefreshing];
                                  [self.tableView.mj_footer endRefreshing];
                              }];
}

-(void)loadMoreData{
    [NetworkAPI revertListWithQuestID:self.quesdata.ID
                               UserId:[UserDefaultHandler getUserDetault:kID]
                             UserKind:[NSString stringWithFormat:@"%d",UserTypeManage]
                              PageNum:[NSString stringWithFormat:@"%ld",(long)self.index]
                             PageSize:@"10"
                              success:^(HXRevertQuestionListModel *result) {
                                  if ([result.code isEqualToString:@"200"]) {
                                      if ([result.anslist count] == 0) {
                                          self.tableView.mj_footer.alpha = 1.0f;
                                          [self.tableView.mj_header endRefreshing];
                                          [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                          return;
                                      }
                                      self.index++;
                                      [self.dataMArray addObjectsFromArray:result.anslist];
                                      [self.tableView reloadData];
                                      [self.tableView.mj_footer endRefreshing];
                                      self.tableView.mj_footer.alpha = 0;
                                  }else if([result.code isEqualToString:@"400"]){//暂无数据
                                      self.tableView.mj_footer.alpha = 1.0f;
                                      [self.tableView.mj_header endRefreshing];
                                      [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                  }else{//查询失败
                                      self.tableView.mj_footer.alpha = 0.0f;
                                      [self.tableView.mj_header endRefreshing];
                                      [self.tableView.mj_footer endRefreshing];
                                  }
                              } failure:^(NSError *err) {
                                  [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                  self.tableView.mj_footer.alpha = 0.0f;
                                  [self.tableView.mj_header endRefreshing];
                                  [self.tableView.mj_footer endRefreshing];
                              }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataMArray.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"revertCell" forIndexPath:indexPath];
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"revertCell"];
    HXRevertQuestionAllListModel *model = self.dataMArray[indexPath.row];
    cell.textLabel.text = model.Content;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@回复",model.CorpName];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - 懒加载
- (NSMutableArray *)dataMArray{
    if (_dataMArray == nil) {
        _dataMArray = [[NSMutableArray alloc]init];
    }
    return _dataMArray;
}



- (void)setQuesdata:(HXNotifyDetailsQuestListModel *)quesdata{
    _quesdata = quesdata;
//    [self.tableView.mj_header beginRefreshing];
}

- (UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    }
    return _tableView;

}
@end
